package com.hd.crdao.activity;

import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.bean.GetMyStoreInfoBean;
import com.hd.crdao.bean.MyStoreInfoBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.hd.crdao.wrapper.GlideImageUtils;
import com.hss01248.dialog.StyledDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import okhttp3.Call;

/**
 *
 *@author Programming
 *@date 2017/6/26 0026
 */
public class MyShopActivity extends BaseSecondActivity implements View.OnClickListener {

    @BindView(R.id.myshop_head)
    ExpandHeadView mMyshopHead;
    @BindView(R.id.storeLogo)
    ImageView mStoreLogo;
    @BindView(R.id.myshop_name)
    TextView mMyshopName;
    @BindView(R.id.myshop_location)
    TextView mMyshopLocation;
    @BindView(R.id.myshop_entrance)
    LinearLayout mMyshopEntrance;
    @BindView(R.id.todaytel)
    TextView mTodaytel;
    @BindView(R.id.todayvisitor)
    TextView mTodayvisitor;
    @BindView(R.id.todaypoint)
    TextView mTodaypoint;
    @BindView(R.id.carsourceNum)
    TextView mCarsourceNum;
    @BindView(R.id.shopsetting)
    TextView mShopsetting;
    @BindView(R.id.domainsetting)
    TextView mDomainsetting;
    @BindView(R.id.openshopcertinfo)
    TextView mOpenshopcertInfo;
    /*@BindView(R.id.commissionService)
    TextView mCommissionService;*/
    @BindView(R.id.publishcarsource)
    TextView mPublicCarSource;
    public String mDomainState;
    public MyStoreInfoBean myStoreInfo;


    @Override
    protected void initView() {
        initData();
        initEvent();
    }


    private void initData() {
        Handler handler = new Handler();
        StyledDialog.buildLoading().setActivity(this).show();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                OkHttpUtils.post().url(Constants.GETMYSTOREINFO_URL)
                        .addParams("sign", Constants.SIGN)
                        .addParams("carBusinessId", SPUtils.getString("businessid", ""))
                        .build().execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        MyToast.show("与服务器连接失败");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        GetMyStoreInfoBean getMyStoreInfoBean = new Gson().fromJson(response, GetMyStoreInfoBean.class);
                        if (getMyStoreInfoBean.getSuccess() == 0) {
                            myStoreInfo = getMyStoreInfoBean.getMyStoreInfo();
                            mMyshopName.setText(myStoreInfo.getStoreName());
                            mMyshopLocation.setText(myStoreInfo.getStoreAddress());
                            GlideImageUtils.loadImage(Constants.IMAGEURL + myStoreInfo.getStoreLogo(),R.drawable.no_photo,true,mStoreLogo);

                            mTodaytel.setText(myStoreInfo.getPhoneTotal());
                            mTodayvisitor.setText(myStoreInfo.getVisitsTotal());
                            mTodaypoint.setText(myStoreInfo.getClicksTotal());
                            mCarsourceNum.setText(myStoreInfo.getCarTotal());
                            mDomainState = myStoreInfo.getDomainState();

                        } else {
                            MyToast.show(getMyStoreInfoBean.getMessage());
                        }


                    }
                });
                StyledDialog.dismissLoading();
            }
        },2000);


    }

    @Override
    protected int setContentView() {
        return R.layout.activity_myshop;
    }


    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mMyshopHead;
    }

    private void initEvent() {
        mMyshopEntrance.setOnClickListener(this);
        mPublicCarSource.setOnClickListener(this);
        mCarsourceNum.setOnClickListener(this);
        mShopsetting.setOnClickListener(this);
        mDomainsetting.setOnClickListener(this);
        mOpenshopcertInfo.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.myshop_entrance:
                ALog.d("进入店铺首页");
                Intent i = new Intent(UIUtils.getContext(), ShopIndexActivity.class);
                i.putExtra("storeId", myStoreInfo.getId());
                startActivity(i);
                break;

            case R.id.publishcarsource:
                Intent enterance = new Intent(UIUtils.getContext(), ReleaseActivity.class);
                enterance.putExtra("isFromMain", false);
                startActivity(enterance);
                break;

            case R.id.carsourceNum:
                startActivity(new Intent(UIUtils.getContext(), MyCarSourceActivity.class));
                break;
            case R.id.shopsetting:
                Intent newintent = new Intent(UIUtils.getContext(), ChangeShopInfoActivity.class);
                startActivity(newintent);
                break;
            case R.id.domainsetting:
                if (TextUtils.equals(mDomainState, "01")) {
                    startActivity(new Intent(UIUtils.getContext(), DomainNameSettingActivity.class));
//                    finish();
                } else if (TextUtils.equals(mDomainState, "09")) {
                    startActivity(new Intent(UIUtils.getContext(), MyDomainActivity.class));
//                    finish();
                }
                break;
            case R.id.openshopcertinfo:
                //进入开店认证的信息页面
                Intent intent = new Intent(UIUtils.getContext(), CertPassActivity.class);
                startActivity(intent);
                break;
       /*     case R.id.commissionService:
                MyToast.show("代办服务,暂未开通");
                break;*/
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        ALog.d("onNewIntent这个方法重新加载数据");
        initData();
    }
}
